package com.redoor.app.stake.workflow.controller;
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.redoor.app.stake.basicdata.commons.BasicDataStatus;
import com.redoor.app.stake.basicdata.hsf.service.impl.StakeServiceImpl;
import com.redoor.app.stake.basicdata.model.BaseStake;
import com.redoor.app.stake.basicdata.model.controllerModel.BaseStakeCriteria;
import com.redoor.app.stake.basicdata.mq.consumerService.ChargePoleVersionMessageListener;
import com.redoor.app.stake.basicdata.mq.consumerService.UpdateModelMessageListener;
import com.redoor.app.stake.basicdata.mq.consumerService.UpdateOrganizationMessageListener;
import com.redoor.app.stake.basicdata.service.BaseRegistrationService;
import com.redoor.app.stake.basicdata.service.BaseStakeService;
import com.redoor.app.stake.workflow.mq.consumerService.FesOperation;
import com.redoornetwork.framework.core.response.ResponseResult;
import com.redoornetwork.framework.core.response.ResultGenerator;
import com.yotouch.app.stake.model.response.ResponseBase;
import com.yotouch.app.stake.model.response.ResponseStake;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.List;

/**
 *  mq的测试方法
 *   
 * @author ZhangMumu  
 * @date 2018/4/11 0011 19:54  
 */
@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    FesOperation fesOperation;
    @Autowired
    StakeServiceImpl stakeService;
    @Autowired
    BaseRegistrationService baseRegistrationService;
    @Autowired
    BaseStakeService baseStakeService;

    /**
     *  申请注册码-测试用方法
     *   生成注册码接口实现逻辑中调用了其他的接口,不能直接测试.所以将相应代码直接复制到这里进行测试
     *
     * @author ZhangMumu
     * @date 2018/4/10 0010 9:34
     * @param
     * @return
     */
    @ApiOperation(value = "申请注册码-测试用方法",notes = "生成注册码接口实现逻辑中调用了其他的接口,不能直接测试.所以将相应代码直接复制到这里进行测试")
    @PostMapping("/registrationApplyTest")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "count", value = "注册数量", dataType = "int", paramType = "query", required = true, defaultValue = "20"),
            @ApiImplicitParam(name = "type", value = "充电桩类型（直流，交流)", dataType = "int", paramType = "query", required = true, defaultValue = "1"),
            @ApiImplicitParam(name = "prOrgCode", value = "产权单位编码", dataType = "string", paramType = "query", required = true, defaultValue = "3000000"),
            @ApiImplicitParam(name = "chargerFeeModelId", value = "计费模型id", dataType = "string", paramType = "query", required = true, defaultValue = "3540617082580002"),
            @ApiImplicitParam(name = "serviceFeeModelId", value = "服务费模型id", dataType = "string", paramType = "query", required = true, defaultValue = "3061516187180055"),
            @ApiImplicitParam(name = "note", value = "申请备注", dataType = "string", paramType = "query", required = false, defaultValue = "测试申请注册码"),
            @ApiImplicitParam(name = "orderId", value = "订单 id", dataType = "string", paramType = "query", required = false, defaultValue = "1"),
            @ApiImplicitParam(name = "vendorName", value = "生产厂家[运维未传该值]", dataType = "string", paramType = "query", required = false, defaultValue = ""),
            @ApiImplicitParam(name = "equipModel", value = "设备型号[运维未传该值]", dataType = "string", paramType = "query", required = false, defaultValue = ""),
            @ApiImplicitParam(name = "userName", value = "创建人", dataType = "string", paramType = "query", required = false, defaultValue = ""),
            @ApiImplicitParam(name = "userToken", value = "用户 token[本地测试传入userUuid]", dataType = "string", paramType = "query", required = false, defaultValue = "029221f3-a28a-47d9-a995-de54a0ffc4f8")
    })
    public ResponseResult registrationApplyTest(
            int count,
            int type,
            String prOrgCode,
            String chargerFeeModelId,
            String serviceFeeModelId,
            String note,
            String orderId,
            String vendorName,
            String equipModel,
            String userName,
            String userToken
    ){
        ResponseBase responseBase = stakeService.genRegisterCode(count, type, prOrgCode, chargerFeeModelId, serviceFeeModelId, note, orderId, vendorName, equipModel, userName, userToken);
        if (0 == responseBase.getErrCode()){
            return ResultGenerator.generatorSuccess();
        }else {
            return ResultGenerator.generatorFail(BasicDataStatus.OTHER_EXCEPTION,responseBase.getErrMsg());
        }
    }
    /**
     *  注册码输入桩时候调用资产方法测试
     *
     * @author ZhangMumu
     * @date 2018/4/14 0014 18:50
     * @param
     * @return
     */
    @ApiOperation(value = "注册码输入桩时候调用资产方法测试")
    @PostMapping("/stakeRegTest")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "registerCode", value = "注册码[注册码管理中找一个未使用的]", dataType = "string", paramType = "query", required = false, defaultValue = ""),
            @ApiImplicitParam(name = "stakeNo", value = "桩编号[新桩编号全为0]", dataType = "string", paramType = "query", required = false, defaultValue = "0000000000000000"),
            @ApiImplicitParam(name = "fingerprint", value = "指纹码[前置调用接口的时候输入]", dataType = "string", paramType = "query", required = false, defaultValue = "fingerprint-test")
    })
    public ResponseResult stakeRegTest(String registerCode,String stakeNo,String fingerprint){
        ResponseStake stakeByRegisterCode = stakeService.getStakeByRegisterCode(registerCode, stakeNo);
        /*上面这一步成功了*/
        if (0 == stakeByRegisterCode.getErrCode()){
            ResponseStake responseStake = stakeService.genStakeNoAndAuthCode(registerCode, fingerprint);
            if (0 == responseStake.getErrCode()){
                /*****begin Description:根据注册码获取对应的桩  author:zhangMumu add 2018/4/15 0015 16:30*****/
                String regUuid = baseRegistrationService.getRegistrationUuidByNumber(registerCode);
                if (StringUtils.isBlank(regUuid)){
                    return ResultGenerator.generatorFail(BasicDataStatus.OTHER_EXCEPTION,"获取注册码uuid失败");
                }else{
                    BaseStakeCriteria stakeCriteria = new BaseStakeCriteria();
                    stakeCriteria.setRegistrationuuid(regUuid);
                    List<BaseStake> baseStakes = baseStakeService.queryStake(stakeCriteria);
                    if (null == baseStakes || 0 == baseStakes.size()){
                        return ResultGenerator.generatorFail(BasicDataStatus.OTHER_EXCEPTION,"未查找到桩");
                    }else {
                        return ResultGenerator.generatorSuccess("stakeNo:"+baseStakes.get(0).getStakeno());
                    }
                }
                /*****end Description:根据注册码获取对应的桩  author:zhangMumu add 2018/4/15 0015 16:30*****/
            }else {
                return ResultGenerator.generatorFail(BasicDataStatus.OTHER_EXCEPTION,responseStake.getErrMsg());
            }
        }else {
            return ResultGenerator.generatorFail(BasicDataStatus.OTHER_EXCEPTION,stakeByRegisterCode.getErrMsg());
        }
    }

    /**  
     *  mq测试,桩调试完成之后生成维护工单
     *   
     * @author ZhangMumu  
     * @date 2018/4/11 0011 19:58  
     * @param   
     * @return   
     */  
    @ApiOperation(value = "前置对桩调试成功,发送mq,资产生成桩维护工单-测试")
    @ApiImplicitParam(name = "body", value = "mq的messageBody", paramType = "query", dataType = "string", required = true, defaultValue = "3340290000000237")
    @PostMapping("/testFesOperation")
    public ResponseResult testFesOperation(String body){
        Calendar calendar = Calendar.getInstance();
        body += ","+calendar.getTimeInMillis();
        Message message = new Message();
        message.setBody(body.getBytes());
        message.setTag("completeDebug_tag");
        message.setTopic("TOPIC_FES_OPERATION");
        Action result = fesOperation.consume(message, new ConsumeContext());
        if (Action.CommitMessage == result){
            return ResultGenerator.generatorSuccess();
        }else {
            return ResultGenerator.generatorFail(BasicDataStatus.OTHER_EXCEPTION,"mq测试失败");
        }
    }

}
